﻿Imports Microsoft.Win32
Imports System.Windows.Forms

Public Class Registration
    Public Shared Sub CommonRegisterFunction(ByVal t As Type)
        Dim titleAttribute As AddinTitleAttribute
        Dim summaryAttribute As AddinSummaryAttribute
        Dim environmentAttribute As AddinEnvironmentCategoryAttribute
        Dim environmentAttributes As AddinEnvironmentCategoryAttribute()
        Dim baseKey As RegistryKey = Nothing
        Dim summaryKey As RegistryKey = Nothing
        Dim implementedCategoriesKey As RegistryKey = Nothing
        Dim environmentCategoriesKey As RegistryKey = Nothing

        titleAttribute = AddinTitleAttribute.GetCustomAttribute(t, GetType(AddinTitleAttribute))
        summaryAttribute = AddinSummaryAttribute.GetCustomAttribute(t, GetType(AddinSummaryAttribute))
        environmentAttributes = AddinEnvironmentCategoryAttribute.GetCustomAttributes(t, GetType(AddinEnvironmentCategoryAttribute))

        If titleAttribute Is Nothing Then
            titleAttribute = New AddinTitleAttribute()
        End If

        If summaryAttribute Is Nothing Then
            summaryAttribute = New AddinSummaryAttribute()
        End If

        Try
            baseKey = Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\{0}", t.GUID.ToString("B")))
            baseKey.SetValue("AutoConnect", 1)
            baseKey.SetValue("409", titleAttribute.Title)
            implementedCategoriesKey = baseKey.CreateSubKey(String.Format("Implemented Categories\{0}", SECATIDS.CATID_SolidEdgeAddIn))

            For Each environmentAttribute In environmentAttributes
                environmentCategoriesKey = baseKey.CreateSubKey(String.Format("Environment Categories\{0}", environmentAttribute.Guid.ToString("B")))

                If Not environmentCategoriesKey Is Nothing Then
                    environmentCategoriesKey.Close()
                End If
            Next

            summaryKey = baseKey.CreateSubKey("Summary")
            summaryKey.SetValue("409", summaryAttribute.Summary)
            summaryKey.Close()
        Catch ex As Exception
            Throw
        Finally
            If Not implementedCategoriesKey Is Nothing Then
                implementedCategoriesKey.Close()
            End If
            If Not summaryKey Is Nothing Then
                summaryKey.Close()
            End If
            If Not baseKey Is Nothing Then
                baseKey.Close()
            End If
        End Try
    End Sub

    Public Shared Sub CommonUnregisterFunction(ByVal t As Type)
        Try
            Registry.ClassesRoot.DeleteSubKeyTree(String.Format("CLSID\{0}", t.GUID.ToString("B")))
        Catch ex As Exception
        End Try
    End Sub
End Class
